package com.oro.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.oro.domain.Banca;
import com.oro.service.StudentService;
import com.oro.util.FacesUtils;

public class NewBankNameValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		String bankName = (String) value;

		StudentService studentService = (StudentService) FacesUtils
				.getBean("studentService");

		Banca bank = studentService.getBankByName(bankName);
		
		if (bank != null) {
			FacesMessage message = new FacesMessage();
			message.setSummary(FacesUtils.getBundleKey("already_exists"));
			throw new ValidatorException(message);
		}

	}

}
